/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import com.opensymphony.xwork2.ActionContext;
import com.restfb.exception.FacebookOAuthException;
import java.math.BigInteger;
import java.util.Map;
import models.AccountBO;
import models.PageBO;
import models.PostBO;
import myconfession.dto.account.Account;
import myconfession.dto.account.Accounts;
import myconfession.dto.page.Page;
import myconfession.dto.page.Pages;
import myconfession.dto.post.Post;
import myconfession.dto.post.PostList;
import myconfession.utils.ConstantProvider;
import myconfession.utils.FacebookUtil;
import myconfession.utils.XMLUtility;

/**
 *
 * @author Liem
 */
public class ApprovePostAction {

    private String id;
    private String returnUrl;
    private String error;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public ApprovePostAction() {
    }

    public String execute() throws Exception {
        Account acc = null;
        Post post = null;
        Page page = null;
        Map session = ActionContext.getContext().getSession();
        String username = (String) session.get("USER").toString();
        acc = AccountBO.getAccountByUsername(username);

        post = PostBO.getPostById(new BigInteger(id));

        page = PageBO.getPageById(post.getPageId());

        int countPosted = PostBO.getNumberOfPosted(post.getPageId());


        if (post != null && acc != null) {
            if (acc.getFbToken() != null) {
                try {
                    if (page.getFbPageId() != null) {
                        if (page.getFbPageId().length() > 0) {
                            String hashtag = page.getHashtag();
                            if (hashtag != null) {
                                if (hashtag.length() > 0) {
                                    hashtag = "#" + hashtag + "_" + countPosted;
                                }
                            }
                            String fbPostId = FacebookUtil.postToPage(page.getFbPageId(), post.getPostContent(), acc.getFbToken(), hashtag);
                            if (fbPostId != null) {
                                PostBO.approvePost(Integer.parseInt(id), fbPostId);
                                return "success";
                            }
                        }
                    }
                    error = "Your facebook page is blank. Please edit in page info.";
                } catch (FacebookOAuthException e) {
                    if (e.getErrorCode() == 200) {
                        error = "Your facebook account has not authorized this action";
                    } else {
                        error = e.getErrorMessage();
                    }
                }
            } else {
                error = "You did not connect your account with facebook. Cannot approve post";
            }
        }
        if (error.length() > 0) {
            returnUrl += "&error=" + error;
        }
        return "fail";
    }
}
